/**
 * Created by shidaiapp on 2017/9/29.
 */

import actionUrls from "../../../../api/ApiUrls";
import ApiBase from "../../../../api/moudles/ApiBase";

function changeModelLoading(self, tiyData) {
  tiyData.tStatus.modalClear.isLoading = false;
  self.$nextTick(function () {
    tiyData.tStatus.modalClear.isLoading = true;
  });
}
/**
 * 初始化
 * @param self
 * @param tiyData
 */
export const init = (self, tiyData) => {
  //resetForm(self, tiyData);
  tiyData.tPage.total = 1;
  tiyData.tPage.startIndex = 0;
  tiyData.tPage.stepNumber = 40;
  tiyData.tPage.currentPage = 1;
 };



/**
 * 从服务器请求列表
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const webList = (self, tiyData) => {
  self.$Spin.show();
  self.$apiChat.messageList({
    "pageSize": tiyData.tPage.stepNumber,
    "pageNum": tiyData.tPage.currentPage,
    "userName":tiyData.tParams.searchParams.userName,
    "userId":tiyData.tParams.searchParams.userId,
    "messageAccount":tiyData.tParams.searchParams.token,
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      tiyData.tData.messageList.length = 0;
      let messagePage = response.result;
      tiyData.tPage.total = messagePage.total;
      tiyData.tPage.stepNumber = messagePage.pageSize;
      messagePage.list.forEach(
        function (item) {
          tiyData.tData.messageList.push(item);
        }
      );
      if (0 == tiyData.tData.messageList.length) {
        tiyData.tData.messageList.splice(0,tiyData.tData.messageList.length)
      }
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};


/**
 * 发送消息推送
 * @param self
 * @param tiyData
 */
export const sendMessage = (self, tiyData) => {

  if(tiyData.tParams.messageSendParam.userId == '' && tiyData.tParams.messageSendParam.token == ''){
    self.$Modal.warning({
      title:"提示",
      content:"请输入用户id或者用户token"
    });
    return
  }

  self.$refs["sendMessageForm"].validate((valid) => {
    if (valid) {
      self.$Spin.show();
      self.$apiChat.sendMessage({
        "userId":tiyData.tParams.messageSendParam.userId,
        "userName":tiyData.tParams.messageSendParam.userName,
        "token":tiyData.tParams.messageSendParam.token,
        "content":tiyData.tParams.messageSendParam.content,
        "title":tiyData.tParams.messageSendParam.title,
      }).then(function (response) {
        self.$Spin.hide();
        if (response.success) {
          self.$Message.success("发送成功");
          tiyData.tStatus.modalMessageSend.isShow = false;
        } else {
          self.$Message.error("发送失败！" + response.errorMessage);
        }
      });
    }
  })

};

/**
 * 消息推送详情
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const messageDetail = (self, tiyData) => {
  self.$Spin.show();
  self.$apiChat.messageDetail({
    "id":tiyData.tParams.messageDetailParam.messageId,
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      let message = response.result;
      tiyData.tStatus.modalMessageDetail.message.id = message.id;
      tiyData.tStatus.modalMessageDetail.message.userId = message.userId;
      tiyData.tStatus.modalMessageDetail.message.messageAccount = message.messageAccount;
      tiyData.tStatus.modalMessageDetail.message.user.name = message.user.name;
      tiyData.tStatus.modalMessageDetail.message.title = message.title;
      tiyData.tStatus.modalMessageDetail.message.content = message.content;
      tiyData.tStatus.modalMessageDetail.isShow = true;
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};

/**
 * 推送消息人的查询
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const msgAccountList = (self, tiyData) => {
  if(tiyData.tParams.messageAccountParam.firstName == ''){
    self.$Modal.warning({
      title:"提示",
      content:"请输入用户名的部分或全部"
    });
    return;
  }
  self.$Spin.show();
  self.$apiChat.msgAccountList({
    "firstName":tiyData.tParams.messageAccountParam.firstName,
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      tiyData.tData.accountList.length = 0;
      let messageAccount = response.result;
      messageAccount.list.forEach(function (item) {
        tiyData.tData.accountList.push(item)
      });
      if(tiyData.tData.accountList.length == 0){
        self.$Message.success("查询没有结果");
      }

    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};

